<?php
/**
 * Created by PhpStorm.
 * User: 44766
 * Date: 2018-11-15
 * Time: 16:55
 */

namespace App\Http\Controllers;


use App\Jobs\Viewer;
use App\Models\Crawler;
use App\Models\Position;
use App\Models\Tag;
use App\Services\CrawlerService\CrawlerService;
use App\Services\CrawlerService\driver\Xinxin21ccnnDriver;
use App\Services\LbsService\LbsService;
use App\Services\TagService\TagService;
use GeoHash;
use Illuminate\Foundation\Bus\DispatchesJobs;

class TestController extends Controller
{
    use DispatchesJobs;

    public function index()
    {
        set_time_limit(500);
//        $server = new CrawlerService(Xinxin21ccnnDriver::class);
//        $list   = $server->setPage(1)->getList();
//        dd($list);
        $view=new Viewer(Xinxin21ccnnDriver::class, 'http://www.21ccnn.com/plugin.php?id=aljzp&act=view&lid=43198');
        $view->handle();


        //        $lbs=new LbsService();
//        $geo=$lbs->address("广东省云浮市新兴县新兴壹份贰烧烤吧");
////        $lbs->point(22.707649,112.236481);
//        dd($lbs->toPoint(),$lbs->toAddress());
//        foreach (Position::all() as $position){
//
//            if($position->address&&!$position->geo){
//                $geo=$lbs->address("广东省云浮市新兴县".$position->address);
//                if($geo->toPoint()){
//                    $position->geo=$geo->toGeo();
//                    list($lat,$lng)=$geo->toPoint();
//                    $position->lat=$lat;
//                    $position->lng=$lng;
//                    $position->save();
//                }
//                sleep(0.1);
//            }
//        }
//        $position=Position::find(105);
//        $lbs=new LbsService();

//        22.709431,112.228783
//        22.69573,112.22536
//        $lbs->point()
        dd('aa');
//        $point1=["23.150587","113.324587"];
//        $point2=["23.147475","113.256405"];
//
//        $geo = GeoHash::encode("23.150587","113.324587");
//        dump($geo) ;
//        dump(strlen($geo));
//        $geo = GeoHash::encode("23.147475","113.256405");
//        dump($geo) ;
//
//        $EARTH_RADIUS = 6370.996; // 地球半径系数
//        $PI = 3.1415926;
//
//        $radLat1 = $point1[0] * $PI / 180.0;
//        $radLat2 = $point2[0] * $PI / 180.0;
//
//        $radLng1 = $point1[1] * $PI / 180.0;
//        $radLng2 = $point2[1] * $PI /180.0;
//
//        $a = $radLat1 - $radLat2;
//        $b = $radLng1 - $radLng2;
//
//        $distance = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
//        $distance = $distance * $EARTH_RADIUS * 1000;
//
//
//            $distance = $distance / 1000;
//
//        dump(round($distance, 2));
//
//        $a = 1;
//        $b = 1;
//
//        $distance = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
//        $distance = $distance * $EARTH_RADIUS * 1000;
//
//
//        $distance = $distance / 1000;
//
//        dump(round($distance, 2));
//
//        list($lat, $lng) = GeoHash::decode($geo);
//        dump($lat.', '.$lng) ;
//        Tag::fixTree();
//        $tag_service = new TagService();
//        $tags        = Tag::whereCustomizable(true)->get();
//        $positions   = Position::all();
//        foreach ($positions as $position) {
////            $tag_service->position($position);
//            $round = rand(0, 3);
//            $ts    = $tags->random(rand(0, 5));
//            foreach ($ts as $t) {
//                $tag_service->add($position, $t);
//            }
//
//        }
        dd('aa');

//        $service = new Xinxin21ccnnDriver();
//        $html    = $service->getContent('http://www.21ccnn.com/plugin.php?id=aljzp&act=view&lid=48203');
//        file_put_contents(storage_path('/logs/html.html'),$html);
//        dd('aa');

//        $p=Position::find(19)->toArray();
//        var_dump($p);
        $content = file_get_contents(storage_path('/logs/html.html'));
        $service = new Xinxin21ccnnDriver();
        $html    = $service->analyzeContent($content)->getDetailData();
        var_dump($html);
        exit();

//        $list    = \DB::table('crawlers')->get();
//        foreach ($list as $item) {
//            $service->getDetail($item->url);
//        }

        $c = Crawler::find(4);
        $service->getDetail($c->url);
    }


}